package com.example.dqhl.recycleviewdemo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.dqhl.recycleviewdemo.R;
import com.example.dqhl.recycleviewdemo.activity.retrofit.GetRequest_Interface;
import com.example.dqhl.recycleviewdemo.activity.retrofit.bean.PostRequest_Interface;
import com.example.dqhl.recycleviewdemo.activity.retrofit.bean.RetrofitBean;
import com.example.dqhl.recycleviewdemo.activity.retrofit.bean.TranslateBean;
import com.example.dqhl.recycleviewdemo.util.Constant;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);

        initData();
    }

    private void initData() {
        //使用Retrofit请求 get
        doGetRequest();

        //使用Retrofit请求 post
        doPostRequest();
    }

    private void doGetRequest() {

        //创建Retrofit实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.retrofitBaseUrl)//设置网络请求的url
                .addConverterFactory(GsonConverterFactory.create())//设置使用Gson解析
                .build();

        // 创建 网络请求接口实例
        GetRequest_Interface request_interface = retrofit.create(GetRequest_Interface.class);

        //对发送请求进行封装
        Call<RetrofitBean> call = request_interface.getCall();

        // 发送网络请求（异步）
        call.enqueue(new Callback<RetrofitBean>() {
            //请求成功回调
            @Override


            public void onResponse(Call<RetrofitBean> call, Response<RetrofitBean> response) {
                response.body().show();
            }

            //请求失败回调
            @Override
            public void onFailure(Call<RetrofitBean> call, Throwable t) {
                System.out.println("链接失败");
            }
        });

    }

    private void doPostRequest() {
//创建Retrofit实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.retrofitTranslateBaseUrl)//设置网络请求的url
                .addConverterFactory(GsonConverterFactory.create())//设置使用Gson解析
                .build();

        // 创建 网络请求接口实例
        PostRequest_Interface request_interface = retrofit.create(PostRequest_Interface.class);

        //对发送请求进行封装
        Call<TranslateBean> call = request_interface.getCall("how old are you");

        // 发送网络请求（异步）
        call.enqueue(new Callback<TranslateBean>() {
            //请求成功回调
            @Override
            public void onResponse(Call<TranslateBean> call, Response<TranslateBean> response) {
                System.out.println(response.body().getTranslateResult().get(0).get(0).getTgt());
            }

            //请求失败回调
            @Override
            public void onFailure(Call<TranslateBean> call, Throwable t) {
                System.out.println("链接失败");
                System.out.println(t.getMessage());
            }
        });
    }
}
